﻿using Db.Models.Base;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TecentCOS.Configs;
using static Db.Enums.BlindWaterMark;

namespace TecentCOS.Models
{
    public class AddWaterMark
    {
        public ExtractType extractType { get; set; }
        public WaterMarkType waterMarkType { get; set; }

        public ImageModel originImg { get; set; }


        public ImageModel watermarkImg { get; set; }

        public ImageModel processedImg { get; set; }

        public string watermarktext { get; set; }


        /// <summary>
        /// 只对全盲水印有效 1 2 3 level 值越大则图片受影响程度越大、盲水印效果越好。
        /// </summary>
        public int level { get; set; }


    }



    public class ImageModel : ICloneable
    {
        /// <summary>
        /// key 文件名
        /// </summary>
        public string ObjectKey { get; set; }

        /// <summary>
        /// 文件流 base64
        /// </summary>
        public string FileData { get; set; }

        [JsonConverter(typeof(StringEnumConverter))]
        /// <summary>
        /// 上传状态
        /// </summary>
        public UploadStatus uploadStatus { get; set; }


        public string PreviewUrl { get; set; }

        public object Clone()
        {
            return this.MemberwiseClone();
        }
        public T ConvertToUploadModel<T>(ImageInfoBase imageInfoBase) where T : UploadModelBase, new()
        {
            T image = new T();
            image.Bucket = CosConfig.BucketName;
            
            image.Id = Guid.NewGuid();
            if (this.ObjectKey.Contains("/"))
            {
                var keyArray = this.ObjectKey.Split('/');
                string nameWithExt = keyArray[(keyArray.Length - 1)];
                var nameArray = nameWithExt.Split('.');
                image.Extension = nameArray[1];
                image.ImageName = nameArray[0];
                image.Path = this.ObjectKey.Substring(0, this.ObjectKey.LastIndexOf('/'));
            }
            else
            {
                image.Path = "";
                var nameArray = this.ObjectKey.Split('.');
                image.Extension = nameArray[1];
                image.ImageName = nameArray[0];
            }
            image.UploadTime = DateTime.Now;
            image.width = imageInfoBase.width;
            image.height = imageInfoBase.height;
            image.photo_rgb = imageInfoBase.photo_rgb;
            image.size = imageInfoBase.size;
            image.md5 = imageInfoBase.md5;
            image.format = imageInfoBase.format;
            return image;
        }

    }
}